import processing.serial.*;
import ddf.minim.*;
import ddf.minim.ugens.*;

Minim          minim;
AudioOutput    audioOut;
AudioPlayer[]  sound = new AudioPlayer[4];
Serial         myPort;

void setup() {
  //init serial port
  String portName = Serial.list()[2];
  println(portName);
  myPort = new Serial(this, portName, 9600);

  initSound();
}

void draw() {
  if (myPort.available()>0) {
    println(myPort.readString().trim());
    int rnd = int(random(0, 4));
    try {
      sound[rnd].play();
      sound[rnd].rewind();
    }
    catch(Exception e){
      
    }
  }
}

void initSound() {
  minim = new Minim(this);
  try {
    for (int i=0; i<4; i++) {
      sound[i] = minim.loadFile((i+1) + ".mp3");
    }
  }
  catch(Exception e) {
  }
}
